<?php
namespace app\admin\controller;

class Log extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','msg','like',"%{$d['keyword']}%"]; }
            if($d['type']!=''){ $map[] = ['and','type','=',$d['type']]; }

            $w  = where($map);
            $rs = $this->db->query("select * from log $w[0] order by id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'type'=>function($v){return c("type.log_type.$v");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);
                return suc($data, $rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        return $this->fetch();
    }

    #删除
    public function del(){
      if(is_post()){
        $id = intval($this->post["id"]);
        $rs = $this->db->delete("log",$id);
        if($rs){
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }else{
            return err("删除失败");
        }
      }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('log',$id);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }
}
